/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.org.kasir.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import com.org.kasir.db.ArrayReader;
import com.org.kasir.db.Db;
import com.org.kasir.service.BeliService;
import com.org.kasir.model.Beli;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ivan
 */
@Service("beliService")
public class BeliServiceImpl implements BeliService {

    public void save(String tanggal, int stock) {
        Db db = null;
        Beli beli = null;
        int c = 1;
        try {
            db = new Db(true);
            beli = new Beli();
            db.setAutoCommit(false);
            db.updateData("insert into t_beli(kode,nama,jumlah,harga,total,transaksi,tanggal)values('" + beli.getKode() + "','" + beli.getNama() + "','" + beli.getJumlah() + "','" + beli.getHarga() + "','" + beli.getTotal() + "','" + c + "','" + tanggal + "')");
            db.updateData("update t_barang set stock='" + stock + "' where kode='" + beli.getKode() + "'");
            db.commit();
            db.setAutoCommit(true);
            db.closer();
        } catch (Exception ex) {
            try {
                db.commit();
                db.setAutoCommit(true);
                try {
                    db.closer();
                } catch (Exception ex1) {
                    ex1.printStackTrace();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            ex.printStackTrace();
        }
    }

    public ArrayList<Beli> getReport(String tanggalAwal, String tanggalAhkir) {
        Db db = null;
        ArrayList<Beli> list = new ArrayList<Beli>();

        try {
            db = new Db(true);
            ArrayList temp = db.getData("select id,kode,nama,jumlah,harga,total,transaksi,tanggal from t_beli where tanggal >='" + tanggalAwal + "' and tanggal <='" + tanggalAhkir + "'");
            db.closer();
            if (temp.size() > 0) {
                for (int x = 0; x < temp.size(); x++) {
                    Beli beli = new Beli();
                    beli.setKode(ArrayReader.getObjectFromArray(temp, x, 1).toString());
                    beli.setNama(ArrayReader.getObjectFromArray(temp, x, 2).toString());
                    beli.setJumlah(Integer.parseInt(ArrayReader.getObjectFromArray(temp, x, 3).toString()));
                    beli.setHarga(Double.parseDouble(ArrayReader.getObjectFromArray(temp, x, 4).toString()));
                    beli.setTotal(Double.parseDouble(ArrayReader.getObjectFromArray(temp, x, 5).toString()));
                    beli.setTransaksi(Integer.parseInt(ArrayReader.getObjectFromArray(temp, x, 6).toString()));
                    beli.setTanggal(ArrayReader.getObjectFromArray(temp, x, 7).toString());
                    list.add(beli);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }
}
